package cn.iocoder.yudao.module.vote.controller.admin.dept;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vote.controller.admin.dept.vo.*;
import cn.iocoder.yudao.module.vote.dal.dataobject.dept.VoteDeptDO;
import cn.iocoder.yudao.module.vote.convert.dept.VoteDeptConvert;
import cn.iocoder.yudao.module.vote.service.dept.VoteDeptService;

@Api(tags = "管理后台 - 部门信息")
@RestController
@RequestMapping("/vote/dept")
@Validated
public class VoteDeptController {

    @Resource
    private VoteDeptService deptService;

    @PostMapping("/create")
    @ApiOperation("创建部门信息")
    @PreAuthorize("@ss.hasPermission('vote:dept:create')")
    public CommonResult<Long> createDept(@Valid @RequestBody VoteDeptCreateReqVO createReqVO) {
        return success(deptService.createDept(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新部门信息")
    @PreAuthorize("@ss.hasPermission('vote:dept:update')")
    public CommonResult<Boolean> updateDept(@Valid @RequestBody VoteDeptUpdateReqVO updateReqVO) {
        deptService.updateDept(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除部门信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('vote:dept:delete')")
    public CommonResult<Boolean> deleteDept(@RequestParam("id") Long id) {
        deptService.deleteDept(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得部门信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('vote:dept:query')")
    public CommonResult<VoteDeptRespVO> getDept(@RequestParam("id") Long id) {
        VoteDeptDO dept = deptService.getDept(id);
        return success(VoteDeptConvert.INSTANCE.convert(dept));
    }

    @GetMapping("/list")
    @ApiOperation("获得部门信息列表")
//    @PreAuthorize("@ss.hasPermission('vote:dept:query')")
    public CommonResult<List<VoteDeptRespVO>> getDeptList(VoteDeptExportReqVO exportReqVO) {
        exportReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<VoteDeptDO> list = deptService.getDeptList(exportReqVO);
        return success(VoteDeptConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得部门信息分页")
    @PreAuthorize("@ss.hasPermission('vote:dept:query')")
    public CommonResult<PageResult<VoteDeptRespVO>> getDeptPage(@Valid VoteDeptPageReqVO pageVO) {
        PageResult<VoteDeptDO> pageResult = deptService.getDeptPage(pageVO);
        return success(VoteDeptConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出部门信息 Excel")
    @PreAuthorize("@ss.hasPermission('vote:dept:export')")
    @OperateLog(type = EXPORT)
    public void exportDeptExcel(@Valid VoteDeptExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<VoteDeptDO> list = deptService.getDeptList(exportReqVO);
        // 导出 Excel
        List<VoteDeptExcelVO> datas = VoteDeptConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "部门信息.xls", "数据", VoteDeptExcelVO.class, datas);
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取部门精简信息列表", notes = "只包含被开启的部门，主要用于前端的下拉选项")
    public CommonResult<List<VoteDeptSimpleRespVO>> getSimpleDepts(VoteDeptExportReqVO reqVO) {
        // 获得部门列表，只要开启状态的
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());

        List<VoteDeptDO> list = deptService.getDeptList(reqVO);
        // 排序后，返回给前端
        list.sort(Comparator.comparing(VoteDeptDO::getSort));
        return success(VoteDeptConvert.INSTANCE.convertSimpleList(list));
    }

}
